package com.chao.yyks.modular.score.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.chao.common.util.ListUtile;
import com.chao.common.util.StrUtile;
import com.chao.common.util.exception.SysException;
import com.chao.yyks.modular.problem.bean.EnProblemOption;
import com.chao.yyks.modular.score.bean.EnAnswer;
import com.chao.yyks.modular.score.mapper.EnAnswerMapper;
import com.chao.yyks.modular.score.service.EnAnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.chao.common.util.reaction.PageList;


/**
 * 成绩答题ServiceImpl
 * chao
 * 2024-09-13
 */
@Service
public class EnAnswerServiceImpl implements EnAnswerService {

	/**
	 * 成绩答题Mapper
	 */
	@Autowired
	private EnAnswerMapper enAnswerMapper;


	/**
	 * 修改成绩答题
	 */
	@Override
	@Transactional
	public void updateEnAnswer(EnAnswer enAnswer){
		EnAnswer enAnswerPojo = enAnswerMapper.selectEnAnswerByScoreId(enAnswer.getScoreId(),enAnswer.getProblemId());
		if(null == enAnswerPojo){
		    throw new SysException("答题失败:未找到记录,请联系老师!");
		}
		// 选择题答案立即评定对错 其他题目 提交后统一评定
		if(!ListUtile.isEmpty(enAnswerPojo.getOptionList())){
			enAnswer.setAnswerTrue("2");
			for (EnProblemOption option:enAnswerPojo.getOptionList()){
				if(option.getOptionTitle().equals(enAnswer.getAnswer()) && "1".equals(option.getOptionTrue())){
					enAnswer.setAnswerTrue("1");
					break;
				}
			}
		}
		enAnswerMapper.updateEnAnswer(enAnswer);
	}

	/**
	 * 查询成绩答题列表
	 */
	@Override
	public EnAnswer selectEnAnswerByScoreId(String scoreId,String problemId){
		return enAnswerMapper.selectEnAnswerByScoreId(scoreId,problemId);
	}

	/**
	 * 分页查询成绩答题列表
	 */
	@Override
	public PageList<EnAnswer> selectEnAnswerByPage(JSONObject json){
		return enAnswerMapper.selectEnAnswerByPage(StrUtile.getPageJson(json));
	}

}